var dateUtil = (function() {
	return {

		fomatDateToHHMMSS: function(timestamp) {
			if(null == timestamp || undefined == timestamp) {
				return '-'
			}

			timestamp = parseInt(timestamp)
			// 将一个精确到毫秒的时间戳转化为年月日时分秒格式
			var date = new Date(timestamp);

			var h = "";
			var m = "";
			var s = "";

			if(date.getHours() < 10) {
				h = "0" + date.getHours() + ":"
			} else {
				h = date.getHours() + ":"
			}

			if(date.getMinutes() < 10) {
				m = "0" + date.getMinutes() + ":"
			} else {
				m = date.getMinutes() + ":"
			}

			if(date.getSeconds() < 10) {
				s = "0" + date.getSeconds()
			} else {
				s = date.getSeconds()
			}

			return h + m + s

		},

		formatDateTimeZero: function(timestamp) {
			if(!timestamp) {
				return '-'
			}
			timestamp = parseInt(timestamp)
			// 将一个精确到毫秒的时间戳转化为年月日时分秒格式
			var date = new Date(timestamp)

			var h = "";
			var m = "";
			var s = "";

			if(date.getHours() < 10) {
				h = "0" + date.getHours() + ":"
			} else {
				h = date.getHours() + ":"
			}

			if(date.getMinutes() < 10) {
				m = "0" + date.getMinutes() + ":"
			} else {
				m = date.getMinutes() + ":"
			}

			if(date.getSeconds() < 10) {
				s = "0" + date.getSeconds()
			} else {
				s = date.getSeconds()
			}

			return h + m + s
		},

		formatDuring: function(mss) {
			var days = parseInt(mss / (1000 * 60 * 60 * 24));
			var hours = parseInt((mss % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
			var minutes = parseInt((mss % (1000 * 60 * 60)) / (1000 * 60));
			var seconds = parseInt((mss % (1000 * 60)) / 1000);
			return hours + ":" + minutes + ":" + seconds;
		},

		formatDateTime: function(timestamp) {
			if(!timestamp) {
				return '-'
			}
			if(isNaN(timestamp)) {
				return timestamp;
			}
			timestamp = parseInt(timestamp)
			// 将一个精确到毫秒的时间戳转化为年月日时分秒格式
			var date = new Date(timestamp)
			var Y = date.getFullYear() + '-'
			var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-'
			var D = date.getDate() + ' '
			var h = date.getHours() + ':'
			var m = date.getMinutes() + ':'
			var s = date.getSeconds()
			return Y + M + D + h + m + s
		
		},

		formatDateHour: function(timestamp) {
			if(!timestamp) {
				return '-'
			}
			timestamp = parseInt(timestamp)
			// 将一个精确到毫秒的时间戳转化为年月日时分秒格式
			var date = new Date(timestamp)
			var Y = date.getFullYear() + '-'
			var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-'
			var D = date.getDate() + ' '
			var h = date.getHours() + "点"
			return Y + M + D + h
		},

		formatDateMonth: function(timestamp) {
			if(!timestamp) {
				return '-'
			}
			timestamp = parseInt(timestamp)
			// 将一个精确到毫秒的时间戳转化为年月日时分秒格式
			var date = new Date(timestamp)
			var Y = date.getFullYear() + '-'
			var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1)
			return Y + M
		},

		formatSessiontime: function(timestamp) {
			//格式化会话的时候时间
			if(!timestamp) {
				return '-'
			}
			if(isNaN(timestamp)) {
				var date = timestamp.substring(0, 19);
				date = date.replace(/-/g, '/'); //必须把日期'-'转为'/'
				timestamp = new Date(date).getTime();
			}
			timestamp = parseInt(timestamp)
			// 将一个精确到毫秒的时间戳转化为年月日时分秒格式
			var date = new Date(timestamp)
			var Y = date.getFullYear() + '-'
			var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-'
			var D = date.getDate() + ' '
			var h = date.getHours() + ':'
			var m = date.getMinutes() + ':'
			var s = date.getSeconds()
			return M + D + h + m + s
		},
		formatDate: function(timestamp) {
			if(!timestamp) {
				return '-'
			}
			// 将一个精确到毫秒的时间戳转化为年-月-日 格式
			var date = new Date(timestamp)
			var Y = date.getFullYear() + '-'
			var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-'
			var D = date.getDate()
			return Y + M + D
		},
		formatTime: function(timestamp) {
			if(!timestamp) {
				return '-'
			}
			// 将秒转换成时分秒
			var h = Math.floor(timestamp / 3600) < 10 ? '0' + Math.floor(timestamp / 3600) : Math.floor(timestamp / 3600)
			var m = Math.floor((timestamp / 60 % 60)) < 10 ? '0' + Math.floor((timestamp / 60 % 60)) : Math.floor((timestamp / 60 % 60))
			var s = Math.floor((timestamp % 60)) < 10 ? '0' + Math.floor((timestamp % 60)) : Math.floor((timestamp % 60))
			return h + ':' + m + ':' + s
		},
		//格式化elementUI的组件`时间`方法
		formatElementUiTime: function(timestamp) {
			if(!timestamp) {
				return '-'
			}
			var timeDate = new Date(timestamp);

			// 将秒转换成时分秒
			var h = timeDate.getHours();
			var m = timeDate.getMinutes();
			var s = timeDate.getSeconds();
			return h + ':' + m + ':' + s
		},
		formatTimePlus: function(timestamp) {
			if(!timestamp) {
				return '-'
			}
			timestamp = parseInt(timestamp)
			// 将一个精确到毫秒的时间戳转化为年月日时分秒格式
			var date = new Date(timestamp)
			var Y = date.getFullYear() + '-'
			var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-'
			var D = date.getDate() + ' '
			var h = date.getHours() + ':'
			var m = date.getMinutes() + ':'
			var s = date.getSeconds()
			return h + m + s
		},
		formatTimer: function(intNum) {
			//将一个int类型的数字转为计时器格式 xxx:xx:xx
			if(!intNum) {
				return "00:00:00";
			}
			var d = parseInt(intNum / (24 * 3600));
			var h = parseInt(intNum / 60 / 60 % 24);
			var m = parseInt(intNum / 60 % 60);
			var s = parseInt(intNum % 60);
			if(h < 10) {
				h = "0" + h;
			}
			if(m < 10) {
				m = "0" + m;
			}
			if(s < 10) {
				s = "0" + s;
			}
			return h + ":" + m + ":" + s;
		},
		judge: function(beginDate, endDate) {
			var dateDiff = endDate.getTime() - beginDate.getTime(); //时间差的毫秒数
			var dayDiff = Math.floor(dateDiff / (24 * 3600 * 1000)); //计算出相差天数
			if(dayDiff > 31) {
				return false
			} else {
				return true
			}
		},
		/**
		 * 把字符串转为日期 必须是yyyy-MM-dd HH:mm:ss
		 */
		stringToDate(s){
			//把字符串转为日期格式
			s = s.replace(/-/g,"/");
			var date = new Date(s);
			return date;
		},
		/**
		 * 判断两个日期是否间隔1年
		 * @param {Object} beginDate
		 * @param {Object} endDate
		 */
		yearJudge: function(beginDate, endDate) {
			var beginDate2Time;
			var endDate2Time;
			if(beginDate instanceof Date){
				//日期类型
				beginDate2Time=beginDate.getTime();
			}else if(typeof(beginDate)=='string'){
				//字符串类型
				beginDate2Time=dateUtil.stringToDate(beginDate).getTime();;
			}
			if(endDate instanceof Date){
				//日期类型
				endDate2Time=endDate.getTime();
			}else if(typeof(endDate)=='string'){
				//字符串类型
				endDate2Time=dateUtil.stringToDate(endDate).getTime();;
			}
			if(!endDate2Time || !endDate2Time){
				return false;
			}
			var dateDiff = endDate2Time- beginDate2Time; //时间差的毫秒数
			var dayDiff = Math.floor(dateDiff / (24 * 3600 * 1000)); //计算出相差天数
			if(dayDiff > 366) {
				return false
			} else {
				return true
			}
		},
		convertDateFromString:function(timestamp){
			if (timestamp) { 
				if(!isNaN(timestamp)){
					return this.formatDateTimeZero(timestamp);
				}
				
				var date = new Date(timestamp.replace(/-/,"/"));
				var h = "";
				var m = "";
				var s = "";
				if(date.getHours() < 10) {
					h = "0" + date.getHours() + ":"
				} else {
					h = date.getHours() + ":"
				}
				if(date.getMinutes() < 10) {
					m = "0" + date.getMinutes() + ":"
				} else {
					m = date.getMinutes() + ":"
				}
				if(date.getSeconds() < 10) {
					s = "0" + date.getSeconds()
				} else {
					s = date.getSeconds()
				}
				return h + m + s
			}
		},
		//客户名称加密
		formatName(name) {
			let newStr;
			if(name.length === 2) {
				newStr = name.substr(0, 1) + "*"; // 通过substr截取字符串从第0位开始截取,截取1个
			} else if(name.length > 2) {
				// 当名字大于2位时
				let char = "";
				for(let i = 0, len = name.length - 2; i < len; i++) {
					// 循环遍历字符串
					char += "*";
				}
				newStr = name.substr(0, 1) + char + name.substr(-1, 1);
			} else {
				newStr = name;
			}
			return newStr;
		},
		//判断当前时间是否在某一时间段内
		timeRange(beginTime, endTime) {
			var returnTime = false
			var strb = beginTime.split(":");
			if(strb.length != 3) {
				return false;
			}

			var stre = endTime.split(":");
			if(stre.length != 3) {
				return false;
			}

			var b = new Date();
			var e = new Date();
			var n = new Date();

			b.setHours(strb[0]);
			b.setMinutes(strb[1]);
			b.setSeconds(strb[2]);
			e.setHours(stre[0]);
			e.setMinutes(stre[1]);
			e.setSeconds(stre[2]);

			if(n.getTime() - b.getTime() > 0 && n.getTime() - e.getTime() < 0) {
				returnTime = true;
			} else {
				returnTime = false;
			}
			return returnTime
		},
	}
})()
export default dateUtil